package com.jason.shader.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.jason.shader.R;

/**
 * @DESC: Shader有五个子类
 *              1.BitmapShader
 *              2.ComposeShader
 *              3.LinearGradient
 *              4.RadiaGradient
 *              5.SweepGradient
 * @Author: Jason
 * @Date: 16/8/31
 * @Time: 23:22
 */

public class ShaderView1 extends View{

    private static final int RECT_SIZE = 400; // 矩形尺寸的一半

    private Paint mPaint; // 画笔
    private int left, top, right, bottom; // 矩形坐标
    private int width;
    private int height;

    public ShaderView1(Context context, AttributeSet attrs) {
        super(context, attrs);

        // 获取屏幕尺寸
        width = getResources().getDisplayMetrics().widthPixels;
        height = getResources().getDisplayMetrics().heightPixels;

        // 获取屏幕中点坐标
        int screenX = width / 2;
        int screenY = height / 2;

        // 计算矩形左上右下坐标值
        left = screenX - RECT_SIZE;
        top = screenY - RECT_SIZE;
        right = screenX + RECT_SIZE;
        bottom = screenY + RECT_SIZE;

        // 实例化画笔
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);

        // 获取位图
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);

        /**
         *  1. Shader.TileMode.REPEAT 重复的意思
         *  2. Shader.TileMode.MIRROR 镜像的意思
         *  3. Shader.TileMode.CLAMP  边缘拉伸的意思
         */

        // 设置着色器
        mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

        // 线性渐变
//        mPaint.setShader(new LinearGradient(left, top, right - RECT_SIZE,
//                bottom - RECT_SIZE, Color.RED, Color.YELLOW, Shader.TileMode.REPEAT));

        // LinearGradient最简单的一个构造方法，参数虽多其实很好理解x0和y0表示渐变的起点坐标而x1和y1则表示渐变的终点坐标
        // ，这两点都是相对于屏幕坐标系而言的，而color0和color1则表示起点的颜色和终点的颜色

        /*mPaint.setShader(new LinearGradient(left, top, right - RECT_SIZE,
                bottom - RECT_SIZE, new int[]{Color.RED, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE},
                new float[]{ 0, 0.1F, 0.5F, 0.7F, 0.8F}, Shader.TileMode.MIRROR));*/

        // SweepGradient 的意思是梯度渐变，也称之为扫描式渐变，因为其效果有点类似雷达的扫描效果
//        mPaint.setShader(new SweepGradient(screenX, screenY, Color.RED, Color.YELLOW));
//        mPaint.setShader(new SweepGradient(screenX, screenY, new int[]{Color.GREEN, Color.WHITE, Color.GREEN}, null));

        // RadialGradient 径向渐变，径向渐变说的简单点就是个圆形中心向四周渐变的效果
//        mPaint.setShader(new RadialGradient(screenX, screenY, 10.0f, Color.RED, Color.YELLOW, Shader.TileMode.MIRROR));

        /**
         *  ComposeShader (Shader shaderA, Shader shaderB, Xfermode mode)
         *  ComposeShader (Shader shaderA, Shader shaderB, PorterDuff.Mode mode)
         *  组合Shader使用
         */


    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 绘制矩形
        canvas.drawRect(0, 0, width, height, mPaint);
    }
}
